//
//  CommentView.m
//  KOFY
//
//  Created by Warren Holybee on 12/4/10.
//  Copyright 2010 Warren Holybee. All rights reserved.
//


#import "CommentView.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
#import	"defines.h"

@implementation CommentView
@synthesize emailText;
@synthesize wheel;
@synthesize scroll;

-(IBAction)back: (id) sender {
	
	[[self parentViewController] dismissModalViewControllerAnimated:NO];
}

#pragma mark Email Stuff

-(IBAction)send: (id) sender {

	SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = SMTP_FROM;
    testMsg.toEmail = SMTP_TO;
    testMsg.relayHost = SMTP_SERVER;
    testMsg.requiresAuth = NO;
	testMsg.wantsSecure = NO;
	testMsg.subject = @"iPhone comment";
	testMsg.delegate = self;
	
	NSLog(@"email text = %@", [emailText text]);
	
	NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
                               [emailText text],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    
	testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
	[emailText resignFirstResponder];
	[self.wheel startAnimating];
	self.scroll.backgroundColor=[UIColor grayColor];
	
	[testMsg send];
	
}

- (void)messageSent:(SKPSMTPMessage *)message
{
    [message release];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank You" 
													message:@"Your comment has been sent." 
												   delegate:self 
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles:nil]; 
	[alert show];
	[alert release];
	[self.wheel stopAnimating];
	self.scroll.backgroundColor=[UIColor whiteColor];
	[self back:self];
	
    NSLog(@"delegate - message sent");
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    [message release];
		self.view.backgroundColor=[UIColor whiteColor];
	[self.wheel stopAnimating];
    
    NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]);
}

	
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[emailText becomeFirstResponder];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[emailText release];
    [super dealloc];
}


@end
